<?php

abstract class bone
{

	public static $version = '1.2';				// 系统版本号

	public static $tables = null;				// 保存表实例
	public static $controllers = null;			// 保存控制器实例
	public static $models = null;				// 保存模型实例
	public static $templates = null;			// 保存模板实例
	public static $configs = null;				// 保存配置文件实例
	public static $menus = null;				// 保存菜单实例
	public static $users = null;				// 保存用户实例
	public static $session = null;				// session

	public static $libs = null;					// 保存库实例, 存放第三方库


	// 获取一个库的实例
	public static function &get( $lib_name, $app_dir=null )
	{
		if(!is_array(self::$libs))
		{
			include_once BONE_ROOT.DS.'system'.DS.'library.php';
			self::$libs = array();
		}

		if(!array_key_exists($lib_name, self::$libs))
		{
			if($app_dir)
			{
				include_once BONE_ROOT.DS.$app_dir.DS.'libraries'.DS.$lib_name.DS.$lib_name.'.php';
			}
			else
			{
				include_once BONE_ROOT.DS.'libraries'.DS.$lib_name.DS.$lib_name.'.php';
			}

			$class_name = 'library_'.$lib_name;
			self::$libs[$lib_name] = new $class_name();
		}
		return self::$libs[$lib_name];
	}

	// 默认使用 mysql数据库实例
	public static function get_db()
	{
		include_once BONE_ROOT.DS.'system'.DS.'database.php';
		return database::get_instance();
	}
	

	// 获取一个配置文件
	public static function &get_config( $config_name = null, $app_dir=null )
	{
		
		if(!is_array(self::$configs))
			self::$configs = array();

		if(!array_key_exists($config_name, self::$configs))
		{
			if($app_dir)
			{
				include_once BONE_ROOT.DS.$app_dir.DS.'configs'.DS.$config_name.'.php';
			}
			else
			{
				include_once BONE_ROOT.DS.'configs'.DS.$config_name.'.php';
			}
			$class_name = 'config_'.$config_name;
			self::$configs[$config_name] = new $class_name();
		}
		return self::$configs[$config_name];
	}
	
	// 获取一个表
	public static function &get_table( $table_name = null, $app_dir=null )
	{
		if(!is_array(self::$tables))
		{
			include_once BONE_ROOT.DS.'system'.DS.'table.php';
			self::$tables = array();
		}

		$table = null;
		if(!array_key_exists($table_name, self::$tables))
		{
			if($app_dir)
			{
				include_once BONE_ROOT.DS.$app_dir.DS.'tables'.DS.$table_name.'.php';
			}
			else
			{
				include_once BONE_ROOT.DS.'tables'.DS.$table_name.'.php';
			}

			$class_name = 'table_'.$table_name;
			$table = new $class_name();
		}
		return $table;
	}



	// 获取一个控制器
	public static function &get_controller( $controller_name = null, $app_dir=null  )
	{
		if(!is_array(self::$controllers))
		{
			include_once BONE_ROOT.DS.'system'.DS.'controller.php';
			self::$controllers = array();
		}

		if(!array_key_exists($controller_name, self::$controllers))
		{
			if($app_dir)
			{
				include_once BONE_ROOT.DS.$app_dir.DS.'controllers'.DS.'theme.php';
				include_once BONE_ROOT.DS.$app_dir.DS.'controllers'.DS.$controller_name.'.php';
			}
			else
			{
				include_once BONE_ROOT.DS.'controllers'.DS.'theme.php';
				include_once BONE_ROOT.DS.'controllers'.DS.$controller_name.'.php';
			}

			$class_name = 'controller_'.$controller_name;
			self::$controllers[$controller_name] = new $class_name();
		}
		return self::$controllers[$controller_name];
	}

	// 获取一个模型
	public static function &get_model( $model_name = null, $app_dir=null )
	{
		if(!is_array(self::$models))
		{
			include_once BONE_ROOT.DS.'system'.DS.'model.php';
			self::$models = array();
		}

		if(!array_key_exists($model_name, self::$models))
		{
			if($app_dir)
			{
				include_once BONE_ROOT.DS.$app_dir.DS.'models'.DS.$model_name.'.php';
			}
			else
			{
				include_once BONE_ROOT.DS.'models'.DS.$model_name.'.php';
			}

			$class_name = 'model_'.$model_name;
			self::$models[$model_name] = new $class_name();
		}
		return self::$models[$model_name];
		
	}

	// 获取一个模板
	public static function &get_template( $template_name = null, $app_dir=null, $theme_name=null )
	{

		if(!is_array(self::$templates))
		{
			include_once BONE_ROOT.DS.'system'.DS.'template.php';
			self::$templates = array();
		}

		if(!$theme_name)
		{
			$config = self::get_config('theme', $app_dir);
			$theme_name = $config->name;
		}

		if(!array_key_exists($template_name, self::$templates))
		{
			if( $app_dir )
			{
				include_once BONE_ROOT.DS.$app_dir.DS.'themes'.DS.$theme_name.DS.'theme.php';
				include_once BONE_ROOT.DS.$app_dir.DS.'templates'.DS.str_replace('.', DS, $template_name).'.php';
			}
			else
			{
				include_once BONE_ROOT.DS.'themes'.DS.$theme_name.DS.'theme.php';
				include_once BONE_ROOT.DS.'templates'.DS.str_replace('.', DS, $template_name).'.php';
			}

			$class_name = 'template_'.array_pop(explode('.',$template_name));
			self::$templates[$template_name] = new $class_name();
		}
		return self::$templates[$template_name];
	}

	// 获取一个菜单
	public static function &get_menu( $menu_name = null, $app_dir=null )
	{
		if(!is_array(self::$menus))
		{
			include_once BONE_ROOT.DS.'system'.DS.'menu.php';
			self::$menus = array();
		}

		if(!array_key_exists($menu_name, self::$menus))
		{

			if( $app_dir )
			{
				include_once BONE_ROOT.DS.$app_dir.DS.'menus'.DS.$menu_name.'.php';
			}
			else
			{
				include_once BONE_ROOT.DS.'menus'.DS.$menu_name.'.php';
			}

			$class_name = 'menu_'.$menu_name;
			self::$menus[$menu_name] = new $class_name();
		}
		return self::$menus[$menu_name];
	}

	// 获取一个用户
	public static function &get_user( $id = 0 )
	{
		
		if(!is_array(self::$users))
			self::$users = array();
		if (!$id)
		{
			$user = self::get_session()->get('user');
			if($user)
				return $user;
			else
			{
				$user = new stdClass();
				$user->id = 0;
				$user->guest = 1;
				return $user;
			}
		}
		else if(!array_key_exists($id, self::$users))
		{
			$table = bone::get_table('user');
			$table->load($id);
		
			$user = new stdClass();
			$user->id = $table->id;
			$user->username = $table->username;
			$user->email = $table->email;
			$user->name = $table->name;
			$user->guest = 0;

			self::$users[$id] = $user;
		}

		return self::$users[$id];
	}

	// 获取 session
	public static function &get_session()
	{
		if (!is_object(self::$session))
		{
			include_once BONE_ROOT.DS.'system'.DS.'session.php';
			self::$session = new session();
		}

		return self::$session;
	}

	public static function get_version()
	{
		return self::$version;
	}

}
